﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.SqlClient;
using HZ.Data;
using HZ.DrugTrade.Model;
namespace HZ.DrugTrade.DAL
{
    public class DAL_PurchaseOrder
    {

        public static IList<HZ.DrugTrade.Model.View.PurchaseOrder> GetPurchaseOrder_Page(HZ.DrugTrade.Model.Query.PurchaseOrder where, int currentpage, int pagesize, out int totalrecord)
        {
            string condition = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition(where, true);

            HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(string.Format("SELECT * FROM View_PurchaseOrder  WHERE 1=1 {0} ", condition), pagesize);
            pdes.CurrentPage = currentpage;
            totalrecord = pdes.TotalCount;

            DataTable dt = pdes.GetSpecailPage();
            return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.PurchaseOrder>(dt);
        }

        /// <summary>
        /// 删除采购单
        /// </summary>
        /// <param name="PurchaseOrder">采购单</param>
        /// <returns></returns>
        public static int DeletePurchaseOrder(HZ.DrugTrade.Model.PurchaseOrder PurchaseOrder)
        {
            //IDataParameter[] paras = new IDataParameter[] { 
            //    new SqlParameter("@in_OrderID",PurchaseOrder.OrderID),
            //    new SqlParameter("@out_success",SqlDbType.Int)
            //};
            //paras[paras.Length - 1].Direction = ParameterDirection.Output;
            //HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(CommandType.StoredProcedure, "usp_PurchaseOrder_Del", paras);
            //return (int)paras[paras.Length - 1].Value;
            int Flag = 0;
            var h = HZ.Data.DbHelperFactory.Create();
            try
            {
                h.BeginTrans();
                string IsExitsql = string.Format(" SELECT  count(*)  FROM TB_PurchaseOrder WHERE OrderID ='{0}'", PurchaseOrder.OrderID);
                string IsSubmitsql = string.Format("  SELECT OrderStatus FROM  TB_PurchaseOrder  WHERE   OrderID = '{0}'", PurchaseOrder.OrderID);
                if (int.Parse(h.ExecuteScalar(IsExitsql).ToString()) > 0 && int.Parse(h.ExecuteScalar(IsSubmitsql).ToString()) == 0)
                {
                    string strsql = string.Format(@" begin  
                                              insert into TB_PurchaseOrderDetail_DEL select * from  TB_PurchaseOrderDetail WHERE   OrderID = '{0}' ;
                                              update TB_PurchaseOrderDetail_DEL set MODIFYTIME = sysdate WHERE   OrderdetailID in (select orderdetailid from TB_PurchaseOrderDetail where orderid ='{0}') ;
                                              insert into TB_PurchaseOrder_DEL select * from TB_PurchaseOrder  WHERE   OrderID = '{0}' ;
                                              update TB_PurchaseOrder_DEL set MODIFYTIME = sysdate WHERE   OrderID = '{0}' ;
                                          end;", PurchaseOrder.OrderID);
                    string sql = string.Format(@" begin  
                                              DELETE  FROM TB_PurchaseOrderDetail  WHERE   OrderID = '{0}' ;
                                              DELETE  FROM TB_PurchaseOrder  WHERE   OrderID = '{0}' ;
                                          end;", PurchaseOrder.OrderID);
                    h.ExecuteNonQuery(strsql);
                    h.ExecuteNonQuery(sql);
                }
                else
                {
                    //h.CommitTrans();
                    return 2;
                }
                h.CommitTrans();
                Flag = 1;
               
                
            }
            catch
            {
                h.RollTrans();
                Flag = 0;
            }
            return Flag;

        }
        /// <summary>
        /// 撤回采购单
        /// </summary>
        /// <param name="PurchaseOrder">采购单</param>
        /// <returns></returns>
        public static int DropPurchaseOrder(HZ.DrugTrade.Model.PurchaseOrder PurchaseOrder)
        {
         
            int Flag = 0;
            var h = HZ.Data.DbHelperFactory.Create();
            try
            {
                h.BeginTrans();
                string strsql = string.Format(@"select count(*) from View_PurchaseOrderDetail where orderdetailstate = 2 and OrderID ='{0}'", PurchaseOrder.OrderID);
                int count = Convert.ToInt32(h.ExecuteScalar(strsql));
                if (count == 0)
                {
                    string IsExitsql = string.Format(" SELECT  count(*)  FROM TB_PurchaseOrder WHERE OrderID ='{0}'", PurchaseOrder.OrderID);
                    string IsSubmitsql = string.Format("  SELECT OrderStatus FROM  TB_PurchaseOrder  WHERE   OrderID = '{0}'", PurchaseOrder.OrderID);
                    if (int.Parse(h.ExecuteScalar(IsExitsql).ToString()) > 0 && int.Parse(h.ExecuteScalar(IsSubmitsql).ToString()) == 1)
                    {
                        string sql = string.Format(@" begin  
                                              Update TB_PurchaseOrderDetail set OrderDetailState=0  WHERE   OrderID = '{0}' ;
                                              Update  TB_PurchaseOrder set OrderStatus=0 WHERE   OrderID = '{0}' ;
                                          end;", PurchaseOrder.OrderID);
                        h.ExecuteNonQuery(sql);
                    }
                    else
                    {
                        h.CommitTrans();
                        return 0;
                    }
                h.CommitTrans();
                Flag = 1;
                }
                else
                {
                    Flag = 2;
                }
            }
            catch
            {
                h.RollTrans();
            }
            return Flag;

        }
    }
}
